package store

// Config 用于配置上下文存储的元数据封送器
// Config is used to configure the metadata marshaler of the context store
type Config struct {
	contextType   TypeGetter
	endpointTypes map[string]TypeGetter
}

// TypeGetter 是一个函数，用于通过返回指向对象实例的指针来确定上下文或端点元数据的具体类型
// TypeGetter is a func used to determine the concrete type of a context or endpoint metadata by returning a pointer to an instance of the object
// 例如：对于DockerContext类型的上下文，相应的TypeGetter应该返回new（DockerContext）
// eg: for a context of type DockerContext,the corresponding TypeGetter should return new (DockerContext)
type TypeGetter func() interface{}

// NamedTypeGetter 是与名称关联的TypeGetter
type NamedTypeGetter struct {
	name       string
	typeGetter TypeGetter
}

// EndpointTypeGetter 返回具有指定名称和getter的NamedTypeGetter
// EndpointTypeGetter returns a NamedTypeGetter with the specified name and getter
func EndpointTypeGetter(name string, getter TypeGetter) NamedTypeGetter {
	return NamedTypeGetter{
		name:       name,
		typeGetter: getter,
	}
}

// NewConfig 创建一个config对象
func NewConfig(contextType TypeGetter, endpoints ...NamedTypeGetter) Config {
	res := Config{
		contextType:   contextType,
		endpointTypes: make(map[string]TypeGetter),
	}
	for _, e := range endpoints {
		res.endpointTypes[e.name] = e.typeGetter
	}
	return res
}
